package com.lianboke.link.ad.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.lianboke.link.ad.R;
import com.lianboke.link.ad.Views.MyListView;
import com.lianboke.link.ad.VolleyUtils.HttpCallBack;
import com.lianboke.link.ad.VolleyUtils.HttpUtil;
import com.lianboke.link.ad.adapter.AdapterShipin;
import com.lianboke.link.ad.app.MyApplication;
import com.lianboke.link.ad.common.CommonParams;
import com.lianboke.link.ad.utils.DeviceUtils;
import com.lianboke.link.ad.utils.MediaUtils;
import com.lianboke.link.ad.utils.SystemExternalFileUtils;
import com.lianboke.link.ad.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 视频播放页面
 */
public class MediaActivity extends BaseActivity {

    private AdapterShipin mAdapterShipin;
    @BindView(R.id.lv_shipin)
    public MyListView mShipinListView;

    @BindView(R.id.tv_shipin)
    public TextView tv_shipin;

    @BindView(R.id.loading_progress)
    public ProgressBar loading_progress;

    private List<String> videoNetworkUrls = new ArrayList<String>();
    private int downLoadFileSize= 0;//需要下载的文件数量

    @Override
    public int getContentViewId() {
        return R.layout.activity_media;
    }

    @Override
    public void initActivityParams(Bundle savedInstanceState) {
        iv_head_back.setVisibility(View.VISIBLE);
        iv_head_back.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_head_title.setText("新手教程");
        initShiPinAdapter();
        getVidesUrlParmas();
    }

    /**
     * 初始化视频播放
     */
    public void initShiPinAdapter(){
        if(mAdapterShipin==null){
            mAdapterShipin = new AdapterShipin(actContext);
        }
        mShipinListView.setAdapter(mAdapterShipin);
        mShipinListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String strUrl = videoNetworkUrls.get(position);
                Log.e("shipin_tag",strUrl);
                if(!TextUtils.isEmpty(strUrl) && strUrl.startsWith("http")){
                    MediaUtils.playerNetUrl(actContext,strUrl.trim());
                }else if(!TextUtils.isEmpty(strUrl) && !strUrl.startsWith("http")){
                    MediaUtils.playeVideoLocalUrl(actContext,"file://"+strUrl.trim(),"video/*");
                }else{
                    ToastUtil.show(actContext,"视频地址错误", Toast.LENGTH_LONG);
                }

            }
        });
    }


        /**
     * 获视频数据
     */
    public void getVidesUrlParmas(){
        showLoginProgress();
        if(videoNetworkUrls!=null){
            videoNetworkUrls.clear();
        }
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("param","shipin");
        params.put("version", DeviceUtils.getVersionName(actContext));
        params.put("nettypename",DeviceUtils.getDeviceAc());
        params.put("city", MyApplication.strCity);
        HttpUtil.getInstance().request_get(CommonParams.URL_WEB_SHIPIN, params, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String datas) {
                if(!TextUtils.isEmpty(datas)){
                    datas = datas.replaceAll("\"","");
                    String[] strUrls = datas.split("\\,");
                    if(strUrls!=null && strUrls.length>0){
                        downLoadFileSize = strUrls.length;
                        for (String msg:strUrls ) {
                            videoNetworkUrls.add(msg);
                        }
                    }
                    //检查信息
                    checkShouZhuanVideos();
                }
            }

            @Override
            public void onFail(String msg) {
                Log.e("file_log","-------getVidesUrlParmas------onFail----:"+msg);
                downLoadFileSize =0;
                if(videoNetworkUrls!=null){
                    videoNetworkUrls.clear();
                }
                checkShouZhuanVideos();
            }
        });
    }
        /**
     * 检查教程视频信息
     */
    public void checkShouZhuanVideos(){
        //核对并创建文件夹
        hideLoginProgress();
       String strDirs =  SystemExternalFileUtils.createDirByExternal("",CommonParams.DownLoadFileParams.FILE_DOWNLOAD_DIRS);
        List<String > filesPath = null;//文件夹下所有文件
        if(!TextUtils.isEmpty(strDirs)){
            //文件夹不为空
            filesPath = SystemExternalFileUtils.getParentFiles(strDirs);
        }
        if(downLoadFileSize>0 && filesPath.size()!=downLoadFileSize){
            //需要下载视频教程
            mAdapterShipin.setDatas(videoNetworkUrls);
            mShipinListView.setVisibility(View.VISIBLE);
        }else if(filesPath.size()>0){
            //展示视频地址列表
            videoNetworkUrls.clear();
            videoNetworkUrls.addAll(filesPath);
            mAdapterShipin.setDatas(videoNetworkUrls);
            mShipinListView.setVisibility(View.VISIBLE);
        }else{
            mShipinListView.setVisibility(View.GONE);
        }
    }

    public void showLoginProgress(){
        if(loading_progress!=null && loading_progress.getVisibility()!=View.VISIBLE){
            loading_progress.setVisibility(View.VISIBLE);
        }
    }
    public void hideLoginProgress(){
        if(loading_progress!=null && loading_progress.getVisibility()!=View.GONE){
            loading_progress.setVisibility(View.GONE);
        }
    }
}
